/**
 * Apr 16, 2010
 */
package org.tomale.id.projects.ui;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.DateTime;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.forms.IManagedForm;
import org.eclipse.ui.forms.editor.FormEditor;
import org.eclipse.ui.forms.editor.FormPage;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.ScrolledForm;
import org.eclipse.ui.forms.widgets.Section;
import org.eclipse.ui.forms.widgets.TableWrapData;
import org.eclipse.ui.forms.widgets.TableWrapLayout;

/**
 * @author ferd
 *
 */
public class ProjectOverviewPage extends FormPage {

	public static final String PAGE_ID = "org.tomale.id.projects.ui.editor.project.overview";
	public static final String PAGE_TITLE = "Overview";
	
	ProjectEditorInput _input;
	
	Text _name;
	Text _desc;
	
	Text _created;
	DateTime _estimatedStart;
	DateTime _estimatedEnd;
	DateTime _actualStart;
	DateTime _actualEnd;
	
	boolean _isDirty = false;
	
	ModifyListener _modifyListener = new ModifyListener() {
		
		@Override
		public void modifyText(ModifyEvent e) {
			updateDirtyState();
		}
	};
	
	public ProjectOverviewPage(FormEditor editor) {
		super(editor, PAGE_ID, PAGE_TITLE);
	}

	@Override
	protected void createFormContent(IManagedForm managedForm) {
		_input = (ProjectEditorInput) getEditorInput();
		
		final ScrolledForm form = managedForm.getForm();
		FormToolkit tk = managedForm.getToolkit();
		Composite body = form.getBody();
		tk.paintBordersFor(body);
		form.setText(this.getTitle());
		
		TableWrapLayout layout = new TableWrapLayout();
		layout.numColumns = 2;
		body.setLayout(layout);
	
		createGeneralSection(body, tk, _input);
		
		createDatesSection(body, tk);
		
		createSummarySection(body, tk);
	}
	
	private void createGeneralSection(Composite parent, FormToolkit toolkit, ProjectEditorInput input){
		Section section = toolkit.createSection(parent,
				Section.DESCRIPTION | Section.TITLE_BAR | Section.EXPANDED);
		section.setText("General Information");
		section.setDescription("This section describes general information about this project.");
		TableWrapData td = new TableWrapData(TableWrapData.FILL);
		section.setLayoutData(td);
		
		Composite comp = toolkit.createComposite(section);
		toolkit.paintBordersFor(comp);
		GridLayout layout = new GridLayout();
		layout.numColumns = 2;
		comp.setLayout(layout);
		
		Label l;
		
		// name
		l = toolkit.createLabel(comp, "&Name");
		l.setLayoutData(new GridData(SWT.FILL,SWT.FILL,false,false));
		
		_name = toolkit.createText(comp, input.getProjectName());
		_name.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,false));
		
		// description
		l = toolkit.createLabel(comp, "&Description");
		l.setLayoutData(new GridData(SWT.FILL,SWT.FILL,false,false));
		
		_desc = toolkit.createText(comp, input.getProjectDescription());
		_desc.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,false));
		
		section.setClient(comp);
	}
	
	private void createSummarySection(Composite parent, FormToolkit toolkit){
		Section section = toolkit.createSection(parent,
				Section.DESCRIPTION | Section.TITLE_BAR | Section.EXPANDED);
		section.setText("Summary Information");
		section.setDescription("This section describes summary information about this project.");
		TableWrapData td = new TableWrapData(TableWrapData.FILL);
		section.setLayoutData(td);
		
		Composite comp = toolkit.createComposite(section);
		toolkit.paintBordersFor(comp);
		GridLayout layout = new GridLayout();
		layout.numColumns = 2;
		comp.setLayout(layout);
		
		Label l;
		
		l = toolkit.createLabel(comp, "Cost");
		l.setLayoutData(new GridData(SWT.FILL,SWT.FILL,false,false));
		
		section.setClient(comp);
	}
	
	private void createDatesSection(Composite parent, FormToolkit toolkit){
		Section section = toolkit.createSection(parent,
				Section.DESCRIPTION | Section.TITLE_BAR | Section.EXPANDED);
		section.setText("Summary Information");
		section.setDescription("This section describes summary information about this project.");
		TableWrapData td = new TableWrapData(TableWrapData.FILL);
		section.setLayoutData(td);
		
		Composite comp = toolkit.createComposite(section);
		toolkit.paintBordersFor(comp);
		GridLayout layout = new GridLayout();
		layout.numColumns = 2;
		comp.setLayout(layout);
		
		Label l;
		
		// date created
		l = toolkit.createLabel(comp, "Created");
		l.setLayoutData(new GridData(SWT.FILL,SWT.FILL,false,false));
		
		_created = toolkit.createText(comp, "");
		_created.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,false));
		
		// estimated start date
		l = toolkit.createLabel(comp, "&Estimated Start");
		l.setLayoutData(new GridData(SWT.FILL,SWT.FILL,false,false));
		
		_estimatedStart = new DateTime(comp, SWT.DATE | SWT.BORDER | SWT.DROP_DOWN);
		toolkit.adapt(_estimatedStart);
		_estimatedStart.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,false));
		
		// estimated end date
		l = toolkit.createLabel(comp, "E&stimated End");
		l.setLayoutData(new GridData(SWT.FILL,SWT.FILL,false,false));
		
		_estimatedEnd = new DateTime(comp, SWT.DATE | SWT.BORDER | SWT.DROP_DOWN);
		toolkit.adapt(_estimatedEnd);
		_estimatedEnd.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,false));
	
		// actual start date
		l = toolkit.createLabel(comp, "&Actual Start");
		l.setLayoutData(new GridData(SWT.FILL,SWT.FILL,false,false));

		_actualStart = new DateTime(comp, SWT.DATE | SWT.BORDER | SWT.DROP_DOWN);
		toolkit.adapt(_actualStart);
		_actualStart.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,false));
		
		// actual end date
		l = toolkit.createLabel(comp, "A&ctual End");
		l.setLayoutData(new GridData(SWT.FILL,SWT.FILL,false,false));
		
		_actualEnd = new DateTime(comp, SWT.DATE | SWT.BORDER | SWT.DROP_DOWN);
		toolkit.adapt(_actualEnd);
		_actualEnd.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,false));
		
		section.setClient(comp);
	}

	private void updateDirtyState(){
		_isDirty = _input.getProjectName() != _name.getText() | 
			_input.getProjectDescription() != _desc.getText();
	}
	
	@Override
	public boolean isDirty() {
		return _isDirty;
	}
}
